package gui.frames;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import net.miginfocom.swing.MigLayout;
import core.persistance.Database;
import core.persistance.Profile;

public class Profiles extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5284834399306249986L;
	
	private Database db;
	private Profile[] profiles = null;
	private JList list;
	private JButton btnDelete;
	private JButton btnAdd;
	private JButton btnEdit;

	private DefaultListModel listModel;

	public Profiles() {
		setTitle("Profiles");
		getContentPane().setLayout(new MigLayout("", "[150px:250px,grow][70px:70px]", "[][][][30px:30px,grow][]"));
		
		try {
			db = new Database();
			profiles = db.getAllProfiles();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		listModel = new DefaultListModel();
		for (Profile p : profiles) {
			listModel.addElement(p);
		}
		list = new JList(listModel);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		
		JScrollPane listScroller = new JScrollPane(list);
		getContentPane().add(listScroller, "cell 0 0 1 4,grow");
		
		
		btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final Profile p = new Profile();
				final ProfilesEditor pe = new ProfilesEditor();
				JButton save = pe.getBtnSave();
				save.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							// TODO Information about empty name field
							if (pe.getName().equals("")) {
								Toolkit.getDefaultToolkit().beep();
								pe.setFocusOnNameTextBox();
								return;
							}
							p.setName(pe.getName());
							p.setUsername(pe.getUsername());
							p.setPassword(pe.getPassword());
							p.setHostname(pe.getHostname());
							p.setPort(pe.getPort());
							db.addProfile(p);
							listModel.addElement(p);
							pe.setVisible(false);
							pe.dispose();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				});
				pe.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				pe.setResizable(false);
				pe.setLocationRelativeTo(getRootPane());
				pe.pack();
				pe.setTitle("Add new profile");
				pe.setVisible(true);
			}
		});
		getContentPane().add(btnAdd, "cell 1 0,grow");
		
		btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final Profile p = (Profile) list.getSelectedValue();
				final ProfilesEditor pe = new ProfilesEditor();
				final int index = list.getSelectedIndex();
				pe.setTextFields(p);
				JButton save = pe.getBtnSave();
				save.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							if (pe.getName().equals("")) {
								Toolkit.getDefaultToolkit().beep();
								pe.setFocusOnNameTextBox();
								return;
							}
							p.setName(pe.getName());
							p.setUsername(pe.getUsername());
							p.setPassword(pe.getPassword());
							p.setHostname(pe.getHostname());
							p.setPort(pe.getPort());
							db.updateProfile(p);
							listModel.remove(index);
							listModel.add(index, p);
							pe.setVisible(false);
							pe.dispose();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (NumberFormatException e1) {
							// TODO
							e1.printStackTrace();
						}
					}
				});
				pe.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				pe.setResizable(false);
				pe.setLocationRelativeTo(getRootPane());
				pe.pack();
				pe.setTitle("Edit "+p);
				pe.setVisible(true);
			}
		});
		getContentPane().add(btnEdit, "cell 1 1,grow");
		
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int index = list.getSelectedIndex();
					Profile p = (Profile) list.getSelectedValue();
					listModel.remove(index);
					db.deleteProfile(p.getId());
					
					int size = listModel.getSize();
					if (size == 0) {
						btnDelete.setEnabled(false);
					} else {
						if (index == listModel.getSize()) {
							index--;
						}
						list.setSelectedIndex(index);
						list.ensureIndexIsVisible(index);
					}
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		getContentPane().add(btnDelete, "cell 1 2,grow");
	}
}